
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thien
 */
public final class TextInput {

    private static long ltimer;
//    private Font fontSmall = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    private static Font fontMedium = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
//    private Font fontChat = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);


    private static char keyCodesLowCase[][] = {{' ', '0'}, {'.', ',', '?', '!', '\'', '\"', '1'}, {'a', 'b', 'c', '2'}, {'d', 'e', 'f', '3'}, {'g', 'h', 'i', '4'}, {'j', 'k', 'l', '5'}, {'m', 'n', 'o', '6'}, {'p', 'q', 'r', 's', '7'}, {'t', 'u', 'v', '8'}, {'w', 'x', 'y', 'z', '9'}};
    private static char keyCodesUpCase[][] = {{' ', '0'}, {'.', ',', '?', '!', '\'', '\"', '1'}, {'A', 'B', 'C', '2'}, {'D', 'E', 'F', '3'}, {'G', 'H', 'I', '4'}, {'J', 'K', 'L', '5'}, {'M', 'N', 'O', '6'}, {'P', 'Q', 'R', 'S', '7'}, {'T', 'U', 'V', '8'}, {'X', 'Y', 'Z', 'W', '9'}};
//    private static char keyCodesNumber[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    private static String username = "";
    private static String password = "";
    private static String passMask = "";
    private static byte pointerUser = 0;
    private static byte pointerPass = 0;

    public static int model = 1;
    private static int lastKey = -1;
    private static int index;

    private static int keypressTimeout = 1000;
    private static int KEY_NUM0 = Canvas.KEY_NUM0;

    public static final byte LOWER_TYPE = 0;
    public static final byte UPPER_TYPE = 1;
    public static final byte PASSWORD_TYPE = 2;
    private static int time = 0;




    public static String getUserName(){
        return username;
    }

    public static String getPassword(){
        return password;
    }

    public static void setUserName(){
        username = "";
        pointerUser = 0;
    }

    public static void setPassword(){
        password = "";
        passMask = "";
        pointerPass = 0;
    }
    
//    public static void drawText(Graphics g,int x, int y,int width,int height,int color) {
//
//        if (fontMedium.stringWidth(username) > width ) {
//            String str = "";
//            int i;
//            if (fontMedium.substringWidth(username, 0, pointerUser) > width ) {
//                i = 0;
//                while (true) {
//                    str = username.substring(i, pointerUser);
//                    if (fontMedium.stringWidth(str) <= width ) {
//                        break;
//                    } else {
//                        i++;
//                    }
//                }
//                Designer.drawString(g, str, 0, 100, color, x+width-15, y+1);
////                g.drawString(str, x+width-15, y + 1, Graphics.RIGHT | Graphics.TOP);
//            } else {
//                i = username.length() - 1;
//                while (true) {
//                    str = username.substring(0, i);
//                    if (fontMedium.stringWidth(str) <= width ) {
//                        break;
//                    }
//                    i--;
//                }
//                Designer.drawString(g, str, 0, 100, color, x+4, y+1);
//
////                g.drawString(str, x + 4, y + 1, 0);
//            }
//        } else {
//            Designer.drawString(g, username, 0, 100, color, x+5, y+1);
////            g.drawString(username, x+5, y + 1, 0);
//        }
//
//    }
//   
    
    public static void drawText(Graphics g,int x, int y,int width,int height,int color) {

        if (Designer.getStringWidth(username, 0, 100) > width ) {
            String str = "";
            int i;
            if (Designer.getStringWidth(username, 0, pointerUser) > width ) {
                i = 0;
                while (true) {
                    str = username.substring(i, pointerUser);
                    if (Designer.getStringWidth(str, 0, 100) <= width ) {
                        break;
                    } else {
                        i++;
                    }
                }
//                Designer.drawString(g, str, 0, 100, Designer.MARGIN_LEFT, true, true, color, x+5, y+1, width, height);
                Designer.drawString(g, str, 0, 100, color, x+5, y+1);
//                g.drawString(str, x+width-15, y + 1, Graphics.RIGHT | Graphics.TOP);
            } else {
                i = username.length() - 1;
                while (true) {
                    str = username.substring(0, i);
                    if (Designer.getStringWidth(str, 0, 100) <= width ) {
                        break;
                    }
                    i--;
                }
//                Designer.drawString(g, str, 0, 100, Designer.MARGIN_LEFT, true, true, color, x+5, y+1, width, height);

                Designer.drawString(g, str, 0, 100, color, x+5, y+1);

//                g.drawString(str, x + 4, y + 1, 0);
            }
        } else {
                 
//            Designer.drawString(g, username, 0, 100, Designer.MARGIN_LEFT, true, true, color, x+5, y+1, width, height);

            Designer.drawString(g, username, 0, 100, color, x+5, y+1);
//            g.drawString(username, x+5, y + 1, 0);
        }

    }

//    public static void drawPass(Graphics g,int x,int y,int width,int height,int color){
//        if (fontMedium.stringWidth(passMask) > width ) {
//            String str = "";
//            int i;
//            String s = passMask.substring(0, pointerPass);
//            if (fontMedium.stringWidth(s) > width ) {
//                i = 0;
//                while (true) {
//                    str = passMask.substring(i, pointerPass);
//                    if (fontMedium.stringWidth(str) <= width ) {
//                        break;
//                    } else {
//                        i++;
//                    }
//                }
//                Designer.drawString(g, str, 0, 100, color, x+width-15, y+1);
//
////                g.drawString(str, x+width-15,y+1, Graphics.RIGHT | Graphics.TOP);
//            }
//            else {
//                i = passMask.length() - 1;
//                while (true) {
//                    str = passMask.substring(0, i);
//                    if (fontMedium.stringWidth(str) <= width ) {
//                        break;
//                    }
//                    i--;
//                }
//                Designer.drawString(g, str, 0, 100, color, x+2, y+1);
//
////                g.drawString(str, x + 2, y + 1, 0);
//            }
//        } else {
//            Designer.drawString(g, passMask, 0, 100, color, x+3, y+1);
//
////            g.drawString(passMask, x+3, y + 1, 0);
//        }
//        time++;
//        if (time == 5) {
//            String str = "";
//            if (passMask.length() > 0) {
//                for (int i = 0; i < passMask.length(); i++) {
//                    str += '*';
//                }
//                passMask = str;
//            }
//            time = 0;
//        }
//
//    }

    public static void drawPass(Graphics g,int x,int y,int width,int height,int color){
        if (Designer.getStringWidth(passMask,0,100) > width ) {
            String str = "";
            int i;
            String s = passMask.substring(0, pointerPass);
            if (Designer.getStringWidth(s,0,100) > width ) {
                i = 0;
                while (true) {
                    str = passMask.substring(i, pointerPass);
                    if (Designer.getStringWidth(str,0,100)  <= width ) {
                        break;
                    } else {
                        i++;
                    }
                }
                Designer.drawString(g, str, 0, 100, color, x+5, y+1);

//                g.drawString(str, x+width-15,y+1, Graphics.RIGHT | Graphics.TOP);
            }
            else {
                i = passMask.length() - 1;
                while (true) {
                    str = passMask.substring(0, i);
                    if (Designer.getStringWidth(passMask,0,100)  <= width ) {
                        break;
                    }
                    i--;
                }
                Designer.drawString(g, str, 0, 100, color, x+5, y+1);

//                g.drawString(str, x + 2, y + 1, 0);
            }
        } else {
            Designer.drawString(g, passMask, 0, 100, color, x+5, y+1);

//            g.drawString(passMask, x+3, y + 1, 0);
        }
        time++;
        if (time == 5) {
            String str = "";
            if (passMask.length() > 0) {
                for (int i = 0; i < passMask.length(); i++) {
                    str += '*';
                }
                passMask = str;
            }
            time = 0;
        }

    }

    private static String delKey(String str, int index) {
        String s = "";
        s += str.substring(0, index - 1);
        s += str.substring(index);
        return s;
    }
    public static String delKey(int type) {
        if(type==LOWER_TYPE||type==UPPER_TYPE){
            if(username.length()>0){
                username = username.substring(0,username.length()-1);
                pointerUser--;
                return username;
            }
        }
        else if(type==PASSWORD_TYPE){
            if(password.length()>0){
                password = password.substring(0, password.length()-1);
                passMask = passMask.substring(0, passMask.length()-1);
                pointerPass--;
                return password;
            }
        }
        return username;
    }
    private static void selectKeyText(char[][] keywords, int key){
        if (lastKey == key) {
            if (System.currentTimeMillis() - ltimer > keypressTimeout) {
                ltimer = System.currentTimeMillis();
                index = 0;

                if (username.length() == pointerUser) {
                    username += keywords[key - KEY_NUM0][index];
                }
                else {
                    String str = "";
                    str += username.substring(0, pointerUser);
                    str += keywords[key - KEY_NUM0][index];
                    str += username.substring(pointerUser);
                    username = str;
                }
                pointerUser++;
                ltimer = System.currentTimeMillis();


            }
            else {
                index++;
                if (index == keywords[key - Canvas.KEY_NUM0].length) {
                    index = 0;
                }

                username = delKey(username, pointerUser);
                pointerUser--;
                if (username.length() == pointerUser) {
                    username += keywords[key - KEY_NUM0][index];
                }
                else {
                    String str = "";
                    str += username.substring(0, pointerUser);
                    str += keywords[key - KEY_NUM0][index];
                    str += username.substring(pointerUser);
                    username = str;
                }
                pointerUser++;
                ltimer = System.currentTimeMillis();

            }
        }
        else {
            index = 0;
            if (username.length() == pointerUser) {
                username += keywords[key - KEY_NUM0][index];
            } else {
                String str = "";
                str += username.substring(0, pointerUser);
                str += keywords[key - KEY_NUM0][index];
                str += username.substring(pointerUser);
                username = str;
            }
            pointerUser++;
            ltimer = System.currentTimeMillis();
        }
        lastKey = key;
    }

    private static void selectPass(char[][] keywords,int key){
        if (lastKey == key) {
            if (System.currentTimeMillis() - ltimer > keypressTimeout) {
                ltimer = System.currentTimeMillis();
                index = 0;
                if (password.length() == pointerPass) {
                    password += keywords[key - KEY_NUM0][index];
                    passMask += keywords[key - KEY_NUM0][index];
                } else {
                    String str = "";
                    str += password.substring(0, pointerPass);
                    str += keywords[key - KEY_NUM0][index];
                    str += password.substring(pointerPass);
                    password = str;
                    password += keywords[key - KEY_NUM0][index];

                    str = "";
                    str += passMask.substring(0, pointerPass);
                    str += keywords[key - KEY_NUM0][index];
                    str += passMask.substring(pointerPass);
                    passMask = str;
                }
                pointerPass++;
                ltimer = System.currentTimeMillis();
            } else {
                index++;
                if (index == keywords[key - Canvas.KEY_NUM0].length) {
                    index = 0;
                }
                password = delKey(password, pointerPass);
                passMask = delKey(passMask, pointerPass);
                pointerPass--;
                if (password.length() == pointerPass) {
                    password += keywords[key - KEY_NUM0][index];
                    passMask += keywords[key - KEY_NUM0][index];
                } else {
                    String str = "";
                    str += password.substring(0, pointerPass);
                    str += keywords[key - KEY_NUM0][index];
                    str += password.substring(pointerPass);
                    password = str;

                    str = "";
                    str += passMask.substring(0, pointerPass);
                    str += keywords[key - KEY_NUM0][index];
                    str += passMask.substring(pointerPass);
                    passMask = str;
                }
                pointerPass++;
                ltimer = System.currentTimeMillis();
            }
        }
        else {
            index = 0;
            if (password.length() == pointerPass) {
                password += keywords[key - KEY_NUM0][index];
                passMask += keywords[key - KEY_NUM0][index];
            } else {
                String str = "";
                str += password.substring(0, pointerPass);
                str += keywords[key - KEY_NUM0][index];
                str += password.substring(pointerPass);
                password = str;
                str = "";
                str += passMask.substring(0, pointerPass);
                str += keywords[key - KEY_NUM0][index];
                str += passMask.substring(pointerPass);
                passMask = str;
            }
            pointerPass++;
            ltimer = System.currentTimeMillis();
        }
        lastKey = key;
    }


    public static void selectKey( int key,int type) {
        if (key < Canvas.KEY_NUM0) {
            return;
        }
        if (key == 49) {
            keypressTimeout = 1200;
        } else {
            keypressTimeout = 900;
        }

        time = 0;
//        ltimer = System.currentTimeMillis();
        switch (type) {
            case LOWER_TYPE:
                selectKeyText(keyCodesLowCase, key);
                return;
            case UPPER_TYPE:
                selectKeyText(keyCodesUpCase, key);
                return;
            case PASSWORD_TYPE:
                selectPass(keyCodesLowCase, key);
                return;
        }
    }

}
